// system.h 
//	All global variables used in Nachos are defined here.
//
// Copyright (c) 1992-1993 The Regents of the University of California.
// All rights reserved.  See copyright.h for copyright notice and limitation 
// of liability and disclaimer of warranty provisions.

#ifndef SYSTEM_H
#define SYSTEM_H

#include "copyright.h"
#include "utility.h"
#include "thread.h"
#include "scheduler.h"
#include "interrupt.h"
#include "stats.h"
#include "timer.h"
#include "synch.h"

// Initialization and cleanup routines
extern void Initialize(int argc, char **argv); 	// Initialization,
						// called before anything else
extern void Cleanup();				// Cleanup, called when
						// Nachos is done.

extern Thread *currentThread;			// the thread holding the CPU
extern Thread *threadToBeDestroyed;  		// the thread that just finished
extern Scheduler *scheduler;			// the ready list
extern Interrupt *interrupt;			// interrupt status
extern Statistics *stats;			// performance metrics
extern Timer *timer;				// the hardware alarm clock
extern int currentID;   // the machineID of message holding the CPU
extern int currentBox;  // the mailbox of message holding the CPU

#ifdef USER_PROGRAM
#include "machine.h"
#include "addrspace.h"
#include "bitmap.h"
#include "ipt.h"
extern Machine* machine;	// user program memory and registers
// Global variables used in Project 2
#define MAX_USER_PROC 1024	// The maximum number of user program
#define MAX_LOCKS 1024		// The maximum number of locks
#define MAX_CVS 1024		// The maximum number of condition variables
#define MAX_MVS 1024		// The maximum number of MV
typedef struct {
	Lock *lock;
	AddrSpace *addr;
	bool isToBeDeleted;
} KernelLock;
typedef struct {
	Condition *condition;
	AddrSpace *addr;
	bool isToBeDeleted;
} KernelCV;
extern BitMap *phyPageMap;	// Manage the physical page
extern Lock *phyPageAllocLock;	// Make sure that only one thread can use the phyPageMap
extern Table *processTable;		// processTable stores the information of user program
extern Lock *processTableLock;	// the lock is used to make the process table exclusive
extern BitMap *lockMap;			// The bitmap manages the locks
extern Lock *lockTableLock;		// The lock for mutual exclusive access the kernel locks
extern KernelLock kernelLocks[MAX_LOCKS];	// The kernel lock table
extern BitMap *conditionMap;	// The bitmap manages the condition variables
extern Lock *conditionTableLock;	// The lock for mutual exclusive access the kernel CVs
extern KernelCV kernelCVs[MAX_CVS];	// The kernel condition table
extern Lock *printLock;			// Lock used in myprintf
extern Lock *threadLock;		// Used in fork system call
extern int currentTLB;	// The current TLB slot needs to be load
extern Lock *iptLock;	// Lock for accessing IPT
extern IPTEntry *ipt;	// The Invert Page Table
#endif

#ifdef FILESYS_NEEDED 		// FILESYS or FILESYS_STUB 
#include "filesys.h"
#include "bitmap.h"
#include "list.h"
extern FileSystem  *fileSystem;
#define SWAP_SIZE 4096
extern OpenFile *swapFile;	// The swap file that stores the evicted pages.
extern Lock *swapFileLock;	// Ensure the mutual exclusive of the swap file
extern BitMap *swapFileMap;	// keep track of the space in the swap file
enum SwapMethod { RANDOM, FIFO };
extern SwapMethod swapMethod;
extern List *swapQueue;
#endif

#ifdef FILESYS
#include "synchdisk.h"
extern SynchDisk   *synchDisk;
#endif

#ifdef NETWORK
#include "post.h"
typedef struct {
	ServerLock *Serverlock;
	AddrSpace *addr;
	bool isToBeDeleted;
} KernelServerLock;
typedef struct {
	ServerCondition *Servercondition;
	AddrSpace *addr;
	bool isToBeDeleted;
} KernelServerCV;
typedef struct {
	int* MV;
	AddrSpace *addr;
	int size;
	char * name;
} KernelServerMV;
extern PostOffice* postOffice;
extern ACKInfor *Replymsg;
//project3part3
extern KernelServerLock kernelServerLocks[MAX_LOCKS];	// The kernel Server lock table
extern BitMap *lockServerMap;			// The bitmap manages the Server locks
extern Lock *lockServerTableLock;
extern BitMap *conditionServerMap;	// The bitmap manages the condition variables
extern Lock *conditionServerTableLock;	// The lock for mutual exclusive access the kernel CVs
extern KernelServerCV kernelServerCVs[MAX_CVS];
extern BitMap *MVServerMap;	// The bitmap manages the condition variables
extern Lock *MVServerTableLock;	// The lock for mutual exclusive access the kernel CVs
extern KernelServerMV kernelServerMVs[MAX_MVS];
#endif

#endif // SYSTEM_H
